﻿@using OA.Core;
@using OA.Web;
@using OA.Model;
@using OA.BLL;
@using OA.AspNetCore.Html;
@using OA.Core.Helper;

@model OA.Model.Voucher
@{
    CustomForm infoCustomForm = ViewBag.CustomForm;
    FormGroup infoFormGroup = ViewBag.FormGroup;
    ViewBag.Title = Model.Title + "---" + infoCustomForm.Name;
    List<ExtendField> ExtendFields = ViewBag.ExtendFields;
    var tables = ExtendFields.Where(m => m.Type == 99).ToList();
    var references = ExtendFields.Where(m => m.Type == 30 || m.Type == 31).ToList();
    Layout = "~/Areas/Member/Views/Shared/_Layout.cshtml";
    List<ApprovalItem> approvalItems = Model.ApprovalItems;
    string type = ViewBag.Type;
}
<script type="text/javascript" src="@ViewBag.OssUrl/front/js/jQuery.print.min.js"></script>
<style type="text/css">
    .layui-table-header {
        text-align: justify;
        text-align-last: justify;
    }
    .info-content{text-align:left;}
    .extendTable td,.extendTable th{text-align:center;}
    body{background-color:#fff;}
</style>
<div class="layui-my-content">
    <p style="width:100%;display:block;font-size:24px;font-weight:bold;text-decoration:underline;text-align:center;padding-top:24px;">@infoCustomForm.Name</p>
    <table class="layui-table">
        <tr>
            <td class="layui-table-header" style="width:85px;">@Html.GetMyLabelControl(model => model.Title)</td>
            <td class="info-content">@Model.Title</td>
        </tr>
        <tr>
            <td class="layui-table-header">@Html.GetMyLabelControl(model => model.Sponsor)</td>
            <td class="info-content">@Model.DepartmentName @Model.PositionName  @Model.SponsorName </td>
        </tr>

        @if (ExtendFields != null && ExtendFields.Count > 0)
        {
            foreach (var item in ExtendFields)
            {
                if (item.Type == 99 || item.Type == 30 || item.Type == 31) { continue; }
                string itemValue = string.Empty;
                if (Model.Result.ContainsKey(item.Attribute))
                {
                    itemValue = Model.Result[item.Attribute];
                }
                <tr>
                    <td class="layui-table-header">@item.Name</td>
                    @if (item.Type == 15 || item.Type == 16)
                    {
                        <td class="info-content">@Html.Raw($"<a href='itemValue' target='_blank'>{CommonHelper.GetFileName(itemValue)}</a>")</td>
                    }
                    else if (item.Type == 17 || item.Type == 18)
                    {
                        List<string> files = JsonHelper.DeserializeList<string>(itemValue);
                        System.Text.StringBuilder sb = new System.Text.StringBuilder();
                        for (int i = 0; i < files.Count; i++)
                        {
                            sb.Append($"<a href='{files[i]}' target='_blank'>{CommonHelper.GetFileName(files[i])}</a> ");
                        }
                        <td class="info-content">@Html.Raw(sb.ToString())</td>
                    }
                    else
                    {
                        <td class="info-content">@itemValue</td>
                    }

                </tr>
            }
        }
        @foreach (var item in references)
        {
            var simpleApprovals = JsonHelper.DeserializeObj<List<SimpleApproval>>(Model.Result[item.Attribute]);
            if (simpleApprovals != null && simpleApprovals.Count > 0)
            {
                <tr>
                    <td>@item.Name</td>
                    <td>
                        <table class="layui-table">
                            <thead><tr><th>单据编号</th><th>单据标题</th></tr></thead>
                            <tbody>
                                @foreach (var simpleApproval in simpleApprovals)
                                {
                                    <tr><td>@simpleApproval.No</td><td>@simpleApproval.Title</td></tr>
                                }
                            </tbody>
                        </table>
                    </td>
                </tr>
            }

        }
    </table>
   
    @foreach (var item in tables)
    {
        List<Dictionary<string, string>> tableValues = new List<Dictionary<string, string>>();
        if (Model.Result.ContainsKey(item.Attribute))
        {
            string tableValueString = Model.Result[item.Attribute];
            if (!string.IsNullOrWhiteSpace(tableValueString))
            {
                tableValues = JsonHelper.DeserializeList<Dictionary<string, string>>(tableValueString);
            }
        }
        if (tableValues == null || tableValues.Count == 0)
        {
            tableValues = new List<Dictionary<string, string>>() { new Dictionary<string, string>() };
        }
        ViewDataDictionary keyValuePairs = new ViewDataDictionary(ViewData);

        keyValuePairs["TableInfo"] = item.DefaultValue;
        keyValuePairs["TableAttribute"] = item.Attribute;
        keyValuePairs["TableName"] = item.Name;
        @Html.Partial("TableInfoView", tableValues, keyValuePairs)
    }
    <table class="layui-table">
        <thead>
            <tr><td colspan="6" style="text-align:center;">审批流程</td></tr>
            <tr>
                <td style="width:32px; text-align:center;">步骤</td>
                <td>审批人</td>
                <td style="width:100px; text-align:center;">达到时间</td>
                <td style="width:72px; text-align:center;">审批意见</td>
                <td>审批结果</td>
                <td style="width:100px; text-align:center;">审批时间</td>
            </tr>
        </thead>
        <tbody>
            @foreach (ApprovalItem item in approvalItems)
            {
                string userName = "";
                <tr>
                    <td class="center">@item.Step</td>
                    <td>
                        @if (!string.IsNullOrWhiteSpace(item.RealEmpID) && item.EmpID != item.RealEmpID)
                        {
                            userName = item.DepartmentName + " " + item.PositionName + " " + item.EmpName + "（" + item.RealEmpName + "代办）";
                        }
                        else
                        {
                            userName = item.DepartmentName + " " + item.PositionName + " " + item.EmpName;
                        }
                        @userName
                    </td>
                    <td>@item.CreateDateTime.ToString3()</td>
                    <td style="text-align:center;">@item.StatusName</td>
                    <td>@item.Explain</td>
                    <td>@item.ApprovalTime.ToString3()</td>
                </tr>
            }
        </tbody>
    </table>
</div>
<div style="text-align:right;margin-bottom:20px;">
    <input type="button" value="打印" class="layui-btn Print" />
    @*<a href="/Member/CustomForm/Print/@Model.ID" target="_blank" class="layui-btn">转pdf打印</a>*@
    @if (Model.Status == 0)
    {
        <a href="/Member/CustomForm/Edit?id=@Model.CustomFormID&itemid=@Model.ID" target="_self" class="layui-btn">编辑</a>
        <input type="button" value="发起审批" class="layui-btn sponsor" />
    }
</div>
<script type="text/javascript">
    layui.use(['layer', 'laypage', 'element'], function () {
        var layer = layui.layer;
        var laypage = layui.laypage;

    });
    $(document).on('click',".Print", function () {
        //Print ele4 with custom options
        $(".layui-my-content").print({
            //Use Global styles
            globalStyles: false,
            //Add link with attrbute media=print
            mediaPrint: false,
            //Custom stylesheet
            stylesheet: "http://fonts.googleapis.com/css?family=Inconsolata",
            //Print in a hidden iframe
            iframe: false,
            //Don't print this
            noPrintSelector: ".avoid-this",
            //Add this at top
            prepend: '<link rel="stylesheet" href="@ViewBag.OssUrl/front/layui/css/layui.css"><style type="text/css">.layui-table td, .layui-table th {position:static;} .layui-table-header {text-align: justify;text-align-last:justify;}</style>',
            //Add this on bottom
            //append: "<span><br/>Buh Bye!</span>",
            //Log to console when printing is done via a deffered callback
            deferred: $.Deferred().done(function () {  })
        });
    });

    $(".sponsor").click(function () {
          layer.confirm('<p>您正在进行单据发起审批操作</p><p style="color:red;">单据号:@Model.No</p>', {
            title: "发起审批确认",
            btn: ['是的,我要发起审批', '不小心点错了'] //按钮
        }, function () {
              $.getJSON("/Member/CustomForm/Sponsor", { formID: @Model.ID, customID: @Model.CustomFormID}, function (result) {
                if (result.State == 200) {
                    layer.alert('申请审批发起成功!', { icon: 1 });
                    window.location.href = window.location.href;
                }
                else {
                    layer.msg(result.Message);
                }
            });
        }, function () {

        });
    });

</script>
